package com.itheima.bos.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.crm.domain.Customer;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class OrderAction extends BaseAction<Order> {
	private String sendAreaInfo;
	private String recAreaInfo;


	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}


	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}


	@Action(value = "order_save", results = { @Result(name = "success", type = "redirect", location = "index.html") })
	public String addOrder() {
		// 发件人地址
		System.out.println(sendAreaInfo+"________________________");
		Area area = new Area();
		String[] sendAddresss = sendAreaInfo.split("/");
		area.setProvince(sendAddresss[0]);
		area.setCity(sendAddresss[1]);
		area.setDistrict(sendAddresss[2]);
		model.setSendArea(area);
		// 收件人地址
		Area area1 = new Area();
		String[] recAddresss = recAreaInfo.split("/");
		area1.setProvince(recAddresss[0]);
		area1.setCity(recAddresss[1]);
		area1.setDistrict(recAddresss[2]);
		model.setRecArea(area1);
		// 订单绑定当前登录客户信息
		Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		System.out.println(customer.getId());
		model.setCustomer_id(customer.getId());
		//将订单信息发送到Bos_Mnan项目进行保存
		WebClient.create("http://localhost:8080")
		.path("bos_management")
		.path("services")
		.path("orderService")
		.path("order_save")
		.type(MediaType.APPLICATION_JSON).post(model);
		return SUCCESS;

	}
	
	
}
